package com.cjd.common.activity

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cjd.common.utils.AppManager
import com.cjd.common.utils.DisplayUtil
import com.cjd.common.utils.NotchScreenUtil
import com.cjd.common.utils.PermissionUtil
import org.greenrobot.eventbus.EventBus

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/7/2
 * description activity 基类
 */
abstract class BaseActivity : AppCompatActivity() {

    open var isImmersion = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (hasTransparentStatus()) transparentStatusBar()

        setContentView(getLayoutResId())
        AppManager.pushActivity(this)
        if (hasEvent())
            EventBus.getDefault().register(this)
        initData()
    }

    override fun onResume() {
        super.onResume()
        hasNotchPadding()?.let {
            setNotchPadding()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        AppManager.popActivity(this)
        if (hasEvent())
            EventBus.getDefault().unregister(this)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        PermissionUtil.onRequestPermissionsResult(this, requestCode, permissions, grantResults)
    }


    /**
     *  是否透明状态栏
     *  @return 默认false
     */
    open fun hasTransparentStatus(): Boolean = false

    /**
     * 设置 状态栏透明  底部导航栏隐藏
     */
    open fun transparentStatusBar() {
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
        }
        supportActionBar?.hide()
    }

    /**
     * 全屏且状态栏透明
     */
    open fun fullScreen() {
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = Color.TRANSPARENT
            window.statusBarColor = Color.TRANSPARENT
        }
        supportActionBar?.hide()
    }


    /**
     * 需要添加 刘海屏 padding 的view  按需使用
     * @return View
     */
    open fun hasNotchPadding(): View? {
        return null
    }

    /**
     * 是否接收eventBus 消息
     * @return 默认 false
     */
    open fun hasEvent(): Boolean {
        return false
    }

    /**
     * 请求获取授权
     * @param listener 授权回调  {deniedPermissionList} 为空表示全部统一
     * @param permissions 可变权限字符串数组
     */
    fun onRuntimePermissionRequest(
        listener: ((deniedPermissionList: List<String>) -> Unit),
        vararg permissions: String
    ) {
        PermissionUtil.requestPermissions(this, listener, *permissions)
    }


    /**
     * 设置状态栏 文字 /图标 颜色
     * @param isLight true 暗色  false 默认
     * @param isFullScreen true 全屏
     */
    fun setStatusBarTextColor(
        isLight: Boolean,
        bgColor: Int = Color.TRANSPARENT,
        isFullScreen: Boolean = true
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (isLight) {
                if (isFullScreen) {
                    isImmersion = true
                    window.decorView.systemUiVisibility =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                }
            } else {
                if (isFullScreen) {
                    isImmersion = true
                    window.decorView.systemUiVisibility =
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                } else {
                    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                window.navigationBarColor = bgColor
                window.statusBarColor = bgColor
            }
        }
    }

    /**
     * 设置刘海屏 边距
     */
    private fun setNotchPadding() {
        val view = hasNotchPadding()
        view?.let {
            var notchHeight = NotchScreenUtil.getNotchHeight(this)
            if (notchHeight <= 0 && isImmersion)
                notchHeight = DisplayUtil.getStatusBarHeight(this)

            if (notchHeight > 0 && view.paddingTop != notchHeight) {
                view.setPadding(
                    view.paddingLeft,
                    notchHeight,
                    view.paddingRight,
                    view.paddingBottom
                )
            }
        }
    }


    /**
     * 获取 xml 资源ID
     */
    protected abstract fun getLayoutResId(): Int

    /**
     * 初始化 view 或其他
     */
    protected abstract fun initData()
}